package com.ygnn.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ygnn.reggie.common.CustomException;
import com.ygnn.reggie.common.R;
import com.ygnn.reggie.dto.DishDto;
import com.ygnn.reggie.dto.SetmealDto;
import com.ygnn.reggie.entity.Category;
import com.ygnn.reggie.entity.Dish;
import com.ygnn.reggie.entity.Setmeal;
import com.ygnn.reggie.entity.SetmealDish;
import com.ygnn.reggie.service.CategoryService;
import com.ygnn.reggie.service.DishService;
import com.ygnn.reggie.service.SetmealDishService;
import com.ygnn.reggie.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/setmeal")
@Slf4j
public class SetmealController {

    @Autowired
    private SetmealService setmealService;

    @Autowired
    private SetmealDishService setmealDishService;

    @Autowired
    private DishService dishService;

    @Autowired
    private CategoryService categoryService;

    @PostMapping
    public R<String> save(@RequestBody SetmealDto setmealDto){

//        log.info("套餐信息:{}",setmealDto);
        setmealService.saveWithDish(setmealDto);

        return R.success("新增套餐成功");
    }

    @GetMapping("/page")
    public R<Page> page(int page, int pageSize, String name)
    {
//        分页构造对象
        Page<Setmeal> pageInfo=new Page<>(page,pageSize);
        LambdaQueryWrapper<Setmeal> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.like(name!=null,Setmeal::getName,name);
        queryWrapper.orderByDesc(Setmeal::getUpdateTime);
        setmealService.page(pageInfo,queryWrapper);


//        构造SetmealDto类型的分页
        Page<SetmealDto> DtoPageInfo=new Page<>(page,pageSize);
        List<Setmeal> records = pageInfo.getRecords();
//        拷贝普通数据
        BeanUtils.copyProperties(pageInfo,DtoPageInfo,"records");


        List<SetmealDto> setmealDtoList=pageInfo.getRecords().stream().map((item)->{

            SetmealDto setmealDto=new SetmealDto();
            BeanUtils.copyProperties(item,setmealDto);

            Long id = item.getCategoryId();
            Category category = categoryService.getById(id);
            if (category!=null) {
                setmealDto.setCategoryName(category.getName());
            }
            return setmealDto;

        }).collect(Collectors.toList());

        DtoPageInfo.setRecords(setmealDtoList);

        return R.success(DtoPageInfo);

    }

//    批量删除
    @DeleteMapping
    public R<String> delete(@RequestParam List<Long> ids)
    {
//            log.info("ids:{}",ids);
            setmealService.deleteWithDish(ids);
            return R.success("删除成功");
    }


//    停售/起售
    @PostMapping("/status/{st}")
    public R<String> stopAndStart(@RequestParam List<Long> ids,@PathVariable int st)
    {

        List<Setmeal> list=ids.stream().map((item)->{
            Setmeal setmeal=new Setmeal();
            setmeal.setId(item);
            setmeal.setStatus(st);
            return setmeal;
        }).collect(Collectors.toList());



        setmealService.updateBatchById(list);

        return R.success("操作成功");
    }


//    根据id修改
    @GetMapping("/{id}")
    public R<SetmealDto> selectById(@PathVariable Long id)
    {
        SetmealDto setmealDto=new SetmealDto();

//        拷贝普通setmeal表数据
        Setmeal setmeal = setmealService.getById(id);
        BeanUtils.copyProperties(setmeal,setmealDto);

//        根据SetmealId查询setmeal_dish表拷贝到Dto里面
        LambdaQueryWrapper<SetmealDish> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(SetmealDish::getSetmealId,id);
        List<SetmealDish> list = setmealDishService.list(queryWrapper);


        setmealDto.setSetmealDishes(list);

//      改动：遍历得到dishId，查询dish表和dish_dlavor表，返回一个dishDto类型的list

        List<DishDto> dishDtoList = list.stream().map((item) -> {
            DishDto dishDto = dishService.selectByIdWithFlavor(item.getDishId());
            return dishDto;
        }).collect(Collectors.toList());

//        遍历循环得到categoryName
        List<DishDto> dishDtoList1 = dishDtoList.stream().map((item) -> {
            DishDto dishDto2 = new DishDto();
            BeanUtils.copyProperties(item, dishDto2, "categoryName");
            Category categoey = categoryService.getById(item.getCategoryId());
            dishDto2.setCategoryName(categoey.getName());
            return dishDto2;
        }).collect(Collectors.toList());

//        给前端dishdto类型的数据
        setmealDto.setDishDtoList(dishDtoList1);

        return R.success(setmealDto);
    }


//    更新数据
    @PutMapping
    public R<String> update(@RequestBody SetmealDto setmealDto){
//        log.info("修改的数据{}",setmealDto);

        setmealService.updateWithDish(setmealDto);
        return R.success("修改成功");

    }

    @GetMapping("/list")
    public R<List<Setmeal>> list(Setmeal setmeal)
    {
        LambdaQueryWrapper<Setmeal> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(setmeal.getCategoryId()!=null,Setmeal::getCategoryId,setmeal.getCategoryId());
        queryWrapper.eq(setmeal.getStatus()!=null,Setmeal::getStatus,setmeal.getStatus());
        queryWrapper.orderByDesc(Setmeal::getUpdateTime);

        List<Setmeal> list = setmealService.list(queryWrapper);
        return R.success(list);
    }


    @GetMapping("/dish/{id}")
    public R<List<SetmealDish>> dishList(@PathVariable Long id)
    {
//        基本数据封装给前端
        LambdaQueryWrapper<SetmealDish> queryWrapper=new LambdaQueryWrapper();
        queryWrapper.eq(SetmealDish::getSetmealId,id);
        List<SetmealDish> list = setmealDishService.list(queryWrapper);

//        需要查询菜品的图片给前端
//        遍历list的daishid查询图片信息
        List<SetmealDish> setmealDishList = list.stream().map((item) -> {
            Dish dish = dishService.getById(item.getDishId());
            item.setImage(dish.getImage());
            return item;

        }).collect(Collectors.toList());


        return R.success(setmealDishList);
    }




}
